

require '地图.存档逻辑.存档装备-套装词条'

--[装备数据]
local items = {}
local pzname = {'普通','精良','史诗','传说'}
local pznamec = {'|cff00ff00精良|r','|cff00ffff精良|r','|cffff00ff史诗|r','|cffff6600传说|r'}
for _,info in ipairs(load_excel('存档数据.xlsx','存档装备')) do
    local name = ('%s-%s'):format(info['前缀'],info['名称'])
    info.tip = ("|cffff9900属性：|r|n%s'ex_tip'"):format(info['属性'])
    info['单词条几率'] = info['单词条几率'] or 0
    info['品质影响'] = info['品质影响'] or 0
    info['词条上限'] = info['词条上限'] or 0

    --解析属性
    info['属性1'] = cd_unpack_attrs(info['属性'],'data')
    info['属性2'] = cd_unpack_attrs(info['1-5级属性'],'zdata')
    info['属性3'] = cd_unpack_attrs(info['6-9级属性'],'xdata')

    items[name] = info

end


--[强化数据]
local function upgrade_item(item)
    local level = item.level or 0
    local tips = {}
    if level>0 then
        for a=1,3 do
            local key = ('zdata%s'):format(a)
            local num = item[key]
            if num then
                item[key] = num * level
            end
        end
        local tip = format_obj(item, item['1-5级属性'])
        table.insert(tips,tip)
    end
    if level>5 then
        for a=1,3 do
            local key = ('xdata%s'):format(a)
            local num = item[key]
            if num then
                item[key] = num * (level - 5)
            end
        end
        local tip = format_obj(item, item['6-9级属性'])
        table.insert(tips,tip)
    end
    item.ex_tip = ''
    if #tips>0 then
        item.ex_tip = ('|n|n|cff00ff00强化属性：|r|n%s'):format(table.concat(tips,'|n'))
    end
end

--[解析装备数据]
local function builder_item(player,info)
    local name = info.name
    local temp = items[name]
    setmetatable(info, {__index = temp})
    local pz = info.pz
    local pname = pzname[pz]
    
    for a=1,3 do
        local key = ('data%s'):format(a)
        local num = info[key]
        if num then
            info[key] = num * (1 + temp[pname])
        end
    end
    info.title = ('%s[%s]'):format(info['名称'],pznamec[pz])
    if info.level>0 then
        info.title = ('%s +%s'):format(info.title,info.level)
    end
    info.date = os.date(info.time or 0)
    info['品质'] = pz
    get_cdct_tip(info)
    upgrade_item(info)
    
    return info
end

--[排序]
local sort_items = function(player)
    local order = player:get_data('存档装备-顺序') or {}
    local data1 = player:get_data('存档装备-背包') or {}
    local infos = {}
    for _,item in ipairs(data1) do
        local class = item['部位']
        if not infos[class] then
            infos[class] = {}
        end
        table.insert(infos[class],item)
    end
    infos['全部'] = data1
    player:set_data('存档装备-显示',infos)
end

--[取下]
ac.sync:event '同步-取下存档装备'(function(_,player,id)
    local data1 = player:get_data('存档装备-背包') or {}
    local data2 = player:get_data('存档装备-携带') or {}
    for a,item in ipairs(data2) do
        if item.id==id then
            local class = item['部位']
            data2[class] = nil
            player:notify('玩家-刷新套装介绍',player)
            player:send_cloud('取下存档装备',id)
            return
        end
    end
end)

--[佩戴]
ac.sync:event '同步-佩戴存档装备'(function(_,player,id)
    local data1 = player:get_data('存档装备-背包') or {}
    local data2 = player:get_data('存档装备-携带') or {}
    for a,item in ipairs(data1) do
        if item.id==id then
            local class = item.class
            local old = data2[class]
            item.in_use = true
            data2[class] = item
            if old then
                old.in_use = nil
            end
            player:notify('玩家-刷新套装介绍',player)
            player:send_cloud('佩戴存档装备',id)
            return
        end
    end
end)


--[删除]
ac.sync:event '同步-分解存档装备'(function(_,player,id)
    local data = player:get_data('存档装备-背包')
    for a,item in ipairs(data) do
        if item.id==id then
            table.remove(data,a)
            sort_items(player)
            player:send_cloud('分解存档装备',id)
            return
        end
    end
end)

--[镶嵌]
ac.sync:event '同步-设置镶嵌参数'(function(_,name,id)
    local data = player:get_data('存档装备-背包')
    for a,item in ipairs(data) do
        if item.id==id then
            player:set_data(name,item)
            return
        end
    end
end)

ac.sync:event '同步-开始镶嵌宝石'(function()
    local item1 = player:get_data('选择-镶嵌装备')
    local item2 = player:get_data('选择-镶嵌宝石')
    
end)

--[拉取]
ac.game:event '云消息-获取存档背包'(function(_,player,list1,list2,order)
    local info1 = {}
    local info2 = {}
    local maps = {}
    for a,info in ipairs(list1) do
        local item = builder_item(player,info)
        info1[a] = item
        maps[item.id] = item
    end

    for key,id in pairs(list2) do
        local item = maps[id]
        item.in_use = true
        info2[key] = item
    end
    
    player:set_data('存档装备-顺序',order)
    player:set_data('存档装备-背包',info1)
    player:set_data('存档装备-携带',info2)
    player:notify('玩家-加载存档装备属性',player)
    sort_items(player)
end)

--[读取属性]
ac.game:event '玩家-加载存档装备属性'(function(_,player)
    local hero = player.hero
    local data = player:get_data('存档装备-携带')
    if data and hero then
        if player:get_data('初始存档装备')==nil then
            for key,item in pairs(data) do
                load_cdsx(hero,item,'属性1')
                if item.level>0 then
                    load_cdsx(hero,item,'属性2')
                end
                if item.level>5 then
                    load_cdsx(hero,item,'属性3')
                end
            end
            player:notify('玩家-加载套装属性',player,hero)
            player:notify('玩家-加载词条属性',player,hero)
        end
    end
end)


ac.game:event '玩家-选择英雄完毕'(function(_,player)
    player:notify('玩家-加载存档装备属性',player)
end)

ac.game:event '游戏-加载完成'(function(_,hard)
    ac.each_player(function(player)
        player:send_cloud('获取存档背包')
    end)
end)
